﻿using System.Collections.Generic;
using Alone.GameObjects;
using Alone.GameObjects.Enemy;
using Microsoft.Xna.Framework;
using System;

namespace Alone.EnemyStates
{
    /// <summary>
    /// Class StrayState
    /// </summary>
    class StrayState : EnemyState, IEnemyState
    {
        float straySpeed = 2f;
        Matrix rotationMatrix;
        Vector3 movementVelocity;

        /// <summary>
        /// Initializes a new instance of the <see cref="StrayState"/> class.
        /// </summary>
        public StrayState() { }

        #region IEnemyState Members
        public override void doAI(Enemy enemy, List<Enemy> enemies, List<BasePlayer> players)
        {
            BasePlayer player = players[0];
            float distance = 1000;

            movementVelocity = new Vector3(0, 0, straySpeed);
            enemy.AnimationController.Speed = (float)(new Random().NextDouble() + 1);
            
            foreach (Enemy friend in enemies)
            {
                if (enemy != friend)
                {
                    if (isNearFriend(enemy.Position, friend.Position) > 0)// Check if this enemy has a friend nearby
                    {
                        //IF very near friend, turn towards his direction, else turn towards him.
                        if (isVeryNearFriend(enemy.Position, friend.Position) > 0)
                        {
                            enemy.Rotation.Y = ChangeDirection(enemy.Rotation.Y, friend.Rotation.Y, .1f);
                        }
                        else
                        {
                            enemy.Rotation.Y = TurnToFace(enemy.Position, friend.Position, enemy.Rotation.Y, 1f);
                        }
                    }
                }
            }

            rotationMatrix = Matrix.CreateRotationY(enemy.Rotation.Y);
            movementVelocity = Vector3.Transform(movementVelocity, rotationMatrix);
			enemy.Position += movementVelocity;

            foreach (BasePlayer p in players)
            {
                float tempDist = Vector3.Distance(enemy.Position, p.Position);
                if (tempDist < distance)
                {
                    distance = tempDist;
                    player = p;
                }
            }

            // Check if enemy is on the heightmap
            if (!isOnPlane(enemy.Position))
            {
                enemy.Rotation.Y = TurnToFace(enemy.Position, player.Position, enemy.Rotation.Y, 1f);
            }

            enemy.Position.X = MathHelper.Clamp(enemy.Position.X, -3400, 3400);
            enemy.Position.Z = MathHelper.Clamp(enemy.Position.Z, -3400, 3400);
        }

        public override float evaluate(Enemy enemy, List<Enemy> enemies, BasePlayer player)
        {
            float fuzzyValue = 0;
            float maxFuzzy = -10;

            // If enemy is far from player, use strayState
            fuzzyValue = 1 - isNearPlayer(enemy.Position, player.Position) - isVeryNearPlayer(enemy.Position, player.Position);

            foreach (Enemy friend in enemies)
            {
                if (enemy != friend)
                {
                    if (isNearFriend(enemy.Position, friend.Position) > 0)
                    {
                        // If friend is near a player, it is more likely to attack.
                        fuzzyValue -= isNearPlayer(friend.Position, player.Position);
                    }
                }
            }

            return fuzzyValue;
        }

        #endregion
    }


}
